package com.neolix.watch.view;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.neolix.watch.R;
import com.neolix.watch.util.FontTypefaceUtil;

import org.joda.time.DateTime;

import java.util.Calendar;

import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.ArrayWheelAdapter;
import kankan.wheel.widget.adapters.NumericWheelAdapter;

import static com.neolix.watch.NeolixApplication.getInstance;

public class CustomDateTimePickerView extends LinearLayout {

    private WheelView year;
    private WheelView month;
    private WheelView day;
    private WheelView hours;
    private WheelView mins;
    private WheelView ampm;

    public CustomDateTimePickerView(Context context) {
        super(context);
        initUi();
    }

    public CustomDateTimePickerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initUi();
    }

    public CustomDateTimePickerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initUi();
    }

    public void setDateTime(DateTime time) {
        year.setCurrentItem(0);
        month.setCurrentItem(time.getMonthOfYear() - 1);
        day.setCurrentItem(time.getDayOfMonth() - 1);
        int hourOfDay = time.getHourOfDay();
        hours.setCurrentItem(hourOfDay > 11 ? hourOfDay - 12 : hourOfDay);
        mins.setCurrentItem(time.getMinuteOfHour());
        ampm.setCurrentItem(time.getHourOfDay() > 11 ? 1 : 0);
    }

    public DateTime getDateTime() {
        return new DateTime(year.getCurrentItem() + DateTime.now().getYear(),
                month.getCurrentItem() + 1, day.getCurrentItem() + 1,
                ampm.getCurrentItem() == 0 ? hours.getCurrentItem() : hours.getCurrentItem() + 12, mins.getCurrentItem());
    }

    protected void initUi() {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View container = inflater.inflate(R.layout.view_customer_datetime_picker, this, true);

        initWheelView(container);
    }

    private void initWheelView(View container) {
        Calendar calendar = Calendar.getInstance();
        year = (WheelView) container.findViewById(R.id.year);
        month = (WheelView) container.findViewById(R.id.month);
        day = (WheelView) container.findViewById(R.id.day);

        int remarkFontSize = 30;

        day.setRemarkText("日");
        day.setRemarkFontSize(remarkFontSize);
        day.setRemarkTextColor(getResources().getColor(R.color.text_color_yellow));

        OnWheelChangedListener listener = new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                updateDays(year, month, day);
            }
        };

        // year
        year.setRemarkText("年");
        year.setRemarkFontSize(remarkFontSize);
        year.setRemarkTextColor(getResources().getColor(R.color.text_wheel_remark));
        year.setWheelBackground(R.drawable.bg_wheel_backgrond);
//        year.setWheelForeground(R.drawable.bg_wheel_front);
        int curYear = calendar.get(Calendar.YEAR);
        NumericWheelAdapter yearAdapter = new NumericWheelAdapter(getInstance(), curYear, curYear + 1);
        yearAdapter.setItemResource(R.layout.wheel_text_item);
        yearAdapter.setItemTextResource(R.id.text);
        yearAdapter.setTextSize(15);
        yearAdapter.setTextColor(getResources().getColor(R.color.white));
        yearAdapter.setFontType(FontTypefaceUtil.getTypefaceOfHelvetica());
        year.setViewAdapter(yearAdapter);
        year.addChangingListener(listener);


        // month
        month.setRemarkText("月");
        month.setRemarkFontSize(remarkFontSize);
        month.setRemarkTextColor(getResources().getColor(R.color.text_wheel_remark));
        month.setWheelBackground(R.drawable.bg_wheel_backgrond);
        NumericWheelAdapter monthAdapter = new NumericWheelAdapter(getInstance(), 1, 12);
        monthAdapter.setItemResource(R.layout.wheel_text_item);
        monthAdapter.setItemTextResource(R.id.text);
        monthAdapter.setTextColor(getResources().getColor(R.color.white));
        monthAdapter.setFontType(FontTypefaceUtil.getTypefaceOfHelvetica());
        month.setViewAdapter(monthAdapter);
        month.addChangingListener(listener);


        //day
        day.setRemarkText("日");
        day.setRemarkFontSize(remarkFontSize);
        day.setRemarkTextColor(getResources().getColor(R.color.text_wheel_remark));
        day.setWheelBackground(R.drawable.bg_wheel_backgrond);
        updateDays(year, month, day);

        //hours
        hours = (WheelView) container.findViewById(R.id.hour);
        hours.setRemarkText("时");
        hours.setRemarkFontSize(remarkFontSize);
        hours.setRemarkTextColor(getResources().getColor(R.color.text_wheel_remark));
        hours.setWheelBackground(R.drawable.bg_wheel_backgrond);
        NumericWheelAdapter hourAdapter = new NumericWheelAdapter(getInstance(), 0, 11);
        hourAdapter.setItemResource(R.layout.wheel_text_item);
        hourAdapter.setItemTextResource(R.id.text);
        hourAdapter.setTextColor(getResources().getColor(R.color.text_color_yellow));
        hourAdapter.setFontType(FontTypefaceUtil.getTypefaceOfHelvetica());
        hours.setViewAdapter(hourAdapter);

        //mins
        mins = (WheelView) container.findViewById(R.id.mins);
        mins.setRemarkText("分");
        mins.setRemarkFontSize(remarkFontSize);
        mins.setRemarkTextColor(getResources().getColor(R.color.text_wheel_remark));
        mins.setWheelBackground(R.drawable.bg_wheel_backgrond);
        NumericWheelAdapter minAdapter = new NumericWheelAdapter(getInstance(), 0, 59);
        minAdapter.setItemResource(R.layout.wheel_text_item);
        minAdapter.setItemTextResource(R.id.text);
        minAdapter.setTextColor(getResources().getColor(R.color.text_color_yellow));
        minAdapter.setFontType(FontTypefaceUtil.getTypefaceOfHelvetica());
        mins.setViewAdapter(minAdapter);
        mins.setCyclic(true);

        //ampm
        ampm = (WheelView) container.findViewById(R.id.ampm);
        ampm.setWheelBackground(R.drawable.bg_wheel_backgrond);
        ArrayWheelAdapter<String> ampmAdapter =
                new ArrayWheelAdapter<String>(getInstance(), new String[]{"AM", "PM"});
        ampmAdapter.setItemResource(R.layout.wheel_text_item);
        ampmAdapter.setItemTextResource(R.id.text);
        ampmAdapter.setTextColor(getResources().getColor(R.color.text_color_yellow));
        ampmAdapter.setTextSize(15);
        ampmAdapter.setFontType(FontTypefaceUtil.getTypefaceOfHelvetica());
        ampm.setViewAdapter(ampmAdapter);
    }

    private void updateDays(WheelView year, WheelView month, WheelView day) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + year.getCurrentItem());
        calendar.set(Calendar.MONTH, month.getCurrentItem());

        int maxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        DateNumericAdapter dayAdapter = new DateNumericAdapter(getInstance(), 1, maxDays, calendar.get(Calendar.DAY_OF_MONTH) - 1);
        dayAdapter.setItemResource(R.layout.wheel_text_item);
        dayAdapter.setItemTextResource(R.id.text);
        dayAdapter.setTextColor(getResources().getColor(R.color.white));
        dayAdapter.setFontType(FontTypefaceUtil.getTypefaceOfHelvetica());
        day.setViewAdapter(dayAdapter);
        int curDay = Math.min(maxDays, day.getCurrentItem() + 1);
        day.setCurrentItem(curDay - 1, true);
    }

    private class DateNumericAdapter extends NumericWheelAdapter {
        // Index of current item
        int currentItem;
        // Index of item to be highlighted
        int currentValue;

        /**
         * Constructor
         */
        public DateNumericAdapter(Context context, int minValue, int maxValue, int current) {
            super(context, minValue, maxValue);
            this.currentValue = current;
        }

        public DateNumericAdapter(Context context, int minValue, int maxValue, int current, String fromat) {
            super(context, minValue, maxValue, fromat);
            this.currentValue = current;
        }

        @Override
        protected void configureTextView(TextView view) {
            super.configureTextView(view);
            if (currentItem == currentValue) {
                view.setTextColor(0xFF0000F0);
            }
            view.setTypeface(Typeface.SANS_SERIF);
        }

        @Override
        public View getItem(int index, View cachedView, ViewGroup parent) {
            currentItem = index;
            return super.getItem(index, cachedView, parent);
        }
    }
}
